﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        public int PageSize = 8;
        private IProductRepository repository;

        public ProductController(IProductRepository productRepository)
        {
            repository = productRepository;
        }

        /*public ActionResult Index()
        {
            return View();
        }*/

        private IEnumerable<string> GetProductCategory()
        {
           // return repository.Products.Select(p => p.Category).Distinct().OrderBy(p => p);
            return GetCategories.AllCategories().OrderBy(c => c.Ranking).Select(c => c.Name);
        }


        public ViewResult ProductsInformation()
        {
            ProductsInformationViewModel viewModel = new ProductsInformationViewModel
                {
                    Categories = GetProductCategory(),
                    Introduction = "常州xxxx是知名的水利系统供应商，提供从基本的xxxxxxxxxx,以及xxxxx综合系统的解决方案。", //暂时不使用
                    BreadCrumb = new Dictionary<string, string>{{"公司首页","/"},{"产品信息",""}},
                    Products = repository.Products.GroupBy(p=>p.Category).Select(k=>k.FirstOrDefault())
                };
            return View(viewModel);
        }

        public ViewResult List(string category, int page = 1)
        {
            //var dictionary = RouteData.Values.Keys;
            ProductsListViewModel viewModel = new ProductsListViewModel
                {
                    //Products = repository.Products.Where(x=>x.Category==category || category==null).OrderBy(x => x.Id).Skip((page - 1)*PageSize).Take(PageSize),
                    Products =  repository.Products.Where(p=>p.Category.Name==category).OrderBy(p=>p.Ranking).Skip((page-1)*PageSize).Take(PageSize), //modified 2013 11 04
                    PagingInfo = new PagingInfo
                        {
                            CurrentPage = page,
                            ItemsPerPage = PageSize,
                            //TotalItems = category==null? repository.Products.Count():repository.Products.Count(x => x.Category==category),
                            TotalItems = category==null?repository.Products.Count():repository.Products.Count(p=>p.Category.Name == category)
                        },
                        CurrentCategory = category,
                        Categories = GetProductCategory(),
                        BreadCrumb = new Dictionary<string, string>{{"公司首页","/"},{"产品信息",Url.Action("ProductsInformation")},{category,""}}
                };
            ViewBag.SelectedCategory = category;
            return View(viewModel);
        }

        /*
        public FileContentResult GetImage(int productId =1)
        {
            Product product = repository.Products.FirstOrDefault(p => p.Id == productId);
            if (product != null&&product.ImageData!=null)
            {
                return File(product.ImageData, product.ImageMimeType);
            }
            else
            {
                return null;
            }
        }
         * */

        public FileResult GetImage(int productId = 1)
        {
            Product product = repository.Products.FirstOrDefault(p => p.Id == productId);
            if (product != null)
            {
                string contentType = product.ImageMimeType;
                //string filename = @"\Content\images\products\" + productId + ".jpg";
                string filename = @"\Content\images\" + product.CategoryId + @"\" + productId + @"\" +
                                  product.ImageToShow + ".jpg";
                return File(filename, contentType);
            }
            else
            {
                return null;
            }
        }

        public ViewResult Item(int productId)
        {
            Product product = repository.Products.FirstOrDefault(p => p.Id == productId);
            if (product != null)
            {
                ItemViewModel viewModel = new ItemViewModel
                    {
                        Categories = GetProductCategory(),
                        Product = product,
                        BreadCrumb = new Dictionary<string, string>{{"公司首页","/"},{"产品信息",Url.Action("ProductsInformation")},{product.Category.Name,Url.Action("List", new{category=product.Category})},{product.Name,""}}
                    };
                //ViewBag.SelectedCategory = product.Category;
                ViewBag.SelectedCategory = product.Category.Name;
                return View(viewModel);
            }
            else
            {
                return null;
            }
        }
    }
}
